const jssdk = require('jweixin-module');

import base from './base.js';

// 微信公众号登录接口
export default {
	...base,

	/* 初始化页面 */
	init() {
		let that = this;
		return new Promise((resolve, reject) => {
			jssdk.ready(() => {
				jssdk.hideOptionMenu();
				jssdk.hideAllNonBaseMenuItem();
				jssdk.hideMenuItems({
					menuList: [
						'menuItem:copyUrl',
						'menuItem:share:timeline',
						'menuItem:share:appMessage',
					]
				});
				resolve(jssdk);
			});
			uni.request({
				method: "post",
				url: that.vm.$u.api.BASE_URL + '/data/api.wechat/jssdk',
				withCredentials: true,
				data: {
					source: location.href
				},
				success(ret) {
					if (Number(ret.statusCode) === 200) {
						if (ret.data.code) {
							jssdk.config(ret.data.data);
						} else {
							reject(ret.info);
						}
					} else {
						reject(ret)
					}
				}
			});
		});
	},

	/* 获取本地用户资料 */
	local() {
		this.init();
		let that = this;
		return new Promise((resolve, reject) => {
			if (typeof WeChatUserInfo === 'object') {
				that.update(WeChatUserInfo), resolve(WeChatUserInfo)
			} else {
				reject();
			}
		});
	},
}
